
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>字符串 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Landen">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-toggle-chapters/toggle.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-image-captions/image-captions.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="数组.html" />
    
    
    <link rel="prev" href="数据类型.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../01html/html简介.html">
            
                <a href="../01html/html简介.html">
            
                    
                    HTML
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../01html/01html.html">
            
                <a href="../01html/01html.html">
            
                    
                    a链接、图片
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../01html/02html.html">
            
                <a href="../01html/02html.html">
            
                    
                    列表、表格、表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../01html/h5新增.html">
            
                <a href="../01html/h5新增.html">
            
                    
                    h5新增
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../02css/css简介.html">
            
                <a href="../02css/css简介.html">
            
                    
                    CSS
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../02css/居中、省略、清浮.html">
            
                <a href="../02css/居中、省略、清浮.html">
            
                    
                    居中、省略、清浮
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../02css/浮动和定位.html">
            
                <a href="../02css/浮动和定位.html">
            
                    
                    浮动和定位
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../02css/变换、过渡、动画.html">
            
                <a href="../02css/变换、过渡、动画.html">
            
                    
                    变换、过渡、动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../02css/sass和rem.html">
            
                <a href="../02css/sass和rem.html">
            
                    
                    sass和rem
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../02css/flex、media、栅格化.html">
            
                <a href="../02css/flex、media、栅格化.html">
            
                    
                    flex、media、栅格化
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="js简介.html">
            
                <a href="js简介.html">
            
                    
                    JavaScript
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="数据类型.html">
            
                <a href="数据类型.html">
            
                    
                    数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.2" data-path="字符串.html">
            
                <a href="字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="数组.html">
            
                <a href="数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="回流和重绘.html">
            
                <a href="回流和重绘.html">
            
                    
                    回流和重绘
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="浏览器的渲染过程.html">
            
                <a href="浏览器的渲染过程.html">
            
                    
                    浏览器的渲染过程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="十大排序算法.html">
            
                <a href="十大排序算法.html">
            
                    
                    十大排序算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="对象的构建.html">
            
                <a href="对象的构建.html">
            
                    
                    对象的构建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="this.html">
            
                <a href="this.html">
            
                    
                    this
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.9" data-path="bind&call&apply.html">
            
                <a href="bind&call&apply.html">
            
                    
                    bind&call&apply
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.10" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.11" data-path="变量提升.html">
            
                <a href="变量提升.html">
            
                    
                    变量提升
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.12" data-path="作用域.html">
            
                <a href="作用域.html">
            
                    
                    作用域
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.13" data-path="对象.html">
            
                <a href="对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.14" data-path="存储.html">
            
                <a href="存储.html">
            
                    
                    存储
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.15" data-path="事件.html">
            
                <a href="事件.html">
            
                    
                    事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.16" data-path="图片预览.html">
            
                <a href="图片预览.html">
            
                    
                    图片预览
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.17" data-path="防抖节流.html">
            
                <a href="防抖节流.html">
            
                    
                    防抖节流
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.18" data-path="同步异步.html">
            
                <a href="同步异步.html">
            
                    
                    同步异步
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.19" data-path="babel入门.html">
            
                <a href="babel入门.html">
            
                    
                    babel入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.20" data-path="nvm.html">
            
                <a href="nvm.html">
            
                    
                    nvm
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.21" data-path="ES6.html">
            
                <a href="ES6.html">
            
                    
                    ES6
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.22" data-path="ES6-ES11.html">
            
                <a href="ES6-ES11.html">
            
                    
                    ES6-ES11
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >字符串</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x65B9;&#x6CD5;">&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x65B9;&#x6CD5;</h2>
<h3 id="1&#x3001;charat">1&#x3001;charAt()</h3>
<p>&#x8FD4;&#x56DE;&#x5728;&#x6307;&#x5B9A;&#x4F4D;&#x7F6E;&#x7684;&#x5B57;&#x7B26;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;HELLO WORLD&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.charAt(<span class="hljs-number">1</span>)); <span class="hljs-comment">// 1s</span>
</code></pre>
<h3 id="2&#x3001;charcodeat">2&#x3001;charCodeAt()</h3>
<p>&#x8FD4;&#x56DE;&#x5728;&#x6307;&#x5B9A;&#x7684;&#x4F4D;&#x7F6E;&#x7684;&#x5B57;&#x7B26;&#x7684;Unicode&#x7F16;&#x7801;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;HELLO WORLD&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.charCodeAt(<span class="hljs-number">1</span>)); <span class="hljs-comment">// 69</span>
</code></pre>
<h3 id="3&#x3001;concat">3&#x3001;concat()</h3>
<p>&#x8FDE;&#x63A5;&#x4E24;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x65B0;&#x7684;&#x5B57;&#x7B26;&#x4E32;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str1 = <span class="hljs-string">&quot;hello&quot;</span>;
<span class="hljs-keyword">var</span> str2 = <span class="hljs-string">&quot; world!&quot;</span>;
<span class="hljs-keyword">var</span> str3 = <span class="hljs-string">&quot;Have a nice day!&quot;</span>
<span class="hljs-built_in">console</span>.log(str1.concat(str2,str3)); <span class="hljs-comment">// hello world!Have a nice day!</span>
</code></pre>
<h3 id="4&#x3001;fromcharcode">4&#x3001;fromCharCode()</h3>
<p>&#x5C06;Unicode&#x7F16;&#x7801;&#x8F6C;&#x6362;&#x6210;&#x5B57;&#x7B26;</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-built_in">String</span>.fromCharCode(<span class="hljs-number">72</span>,<span class="hljs-number">69</span>,<span class="hljs-number">76</span>,<span class="hljs-number">76</span>,<span class="hljs-number">79</span>)); <span class="hljs-comment">// HELLO</span>
</code></pre>
<h3 id="5&#x3001;indexof">5&#x3001;indexOf()</h3>
<p>&#x8FD4;&#x56DE;&#x67D0;&#x4E2A;&#x6307;&#x5B9A;&#x5B57;&#x7B26;&#x4E32;&#x503C;&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x9996;&#x6B21;&#x51FA;&#x73B0;&#x7684;&#x4F4D;&#x7F6E;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;Hello world, welcome to the universe.&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.indexOf(<span class="hljs-string">&quot;w&quot;</span>)); <span class="hljs-comment">// 6</span>
<span class="hljs-built_in">console</span>.log(str.indexOf(<span class="hljs-string">&quot;w&quot;</span>,<span class="hljs-number">7</span>)); <span class="hljs-comment">// 13</span>
</code></pre>
<h3 id="6&#x3001;includes">6&#x3001;includes()</h3>
<p>&#x67E5;&#x627E;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x5305;&#x542B;&#x6307;&#x5B9A;&#x7684;&#x5B50;&#x5B57;&#x7B26;&#x4E32;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;Hello world, welcome to the Runoob&#x3002;&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.includes(<span class="hljs-string">&quot;world&quot;</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(str.includes(<span class="hljs-string">&quot;world&quot;</span>, <span class="hljs-number">7</span>)); <span class="hljs-comment">// false</span>
</code></pre>
<h3 id="7&#x3001;lastindexof">7&#x3001;lastIndexOf()</h3>
<p>&#x4ECE;&#x540E;&#x5148;&#x524D;&#x641C;&#x7D22;&#x5B57;&#x7B26;&#x4E32;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;I am from runoob&#xFF0C;welcome to runoob site.&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.lastIndexOf(<span class="hljs-string">&quot;m&quot;</span>)); <span class="hljs-comment">// 22</span>
<span class="hljs-comment">// &#x4ECE;&#x4E0B;&#x6807;&#x4E3A;21&#x5F00;&#x59CB;&#x5F80;&#x56DE;&#x6570;</span>
<span class="hljs-built_in">console</span>.log(str.lastIndexOf(<span class="hljs-string">&quot;m&quot;</span>, <span class="hljs-number">21</span>)); <span class="hljs-comment">// 8</span>
</code></pre>
<h3 id="8&#x3001;match">8&#x3001;match()</h3>
<p>&#x67E5;&#x627E;&#x627E;&#x5230;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x5339;&#x914D;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;The rain in SPAIN stays mainly in the plain&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.match(<span class="hljs-regexp">/ain/</span>)); <span class="hljs-comment">// [&quot;ain&quot;, index: 5, input: &quot;The rain in SPAIN stays mainly in the plain&quot;, groups: undefined]</span>
<span class="hljs-comment">// g&#x5339;&#x914D;&#x6240;&#x6709;</span>
<span class="hljs-built_in">console</span>.log(str.match(<span class="hljs-regexp">/ain/g</span>)); <span class="hljs-comment">// [&quot;ain&quot;, &quot;ain&quot;, &quot;ain&quot;]</span>
<span class="hljs-comment">// i&#x4E0D;&#x533A;&#x5206;&#x5927;&#x5C0F;&#x5199;</span>
<span class="hljs-built_in">console</span>.log(str.match(<span class="hljs-regexp">/ain/gi</span>)); <span class="hljs-comment">// [&quot;ain&quot;, &quot;AIN&quot;, &quot;ain&quot;, &quot;ain&quot;]</span>
</code></pre>
<h3 id="9&#x3001;repeat">9&#x3001;repeat()</h3>
<p>&#x590D;&#x5236;&#x5B57;&#x7B26;&#x4E32;&#x6307;&#x5B9A;&#x6B21;&#x6570;&#xFF0C;&#x5E76;&#x5C06;&#x5B83;&#x4EEC;&#x8FDE;&#x63A5;&#x5728;&#x4E00;&#x8D77;&#x8FD4;&#x56DE;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;Runoob&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.repeat(<span class="hljs-number">2</span>)); <span class="hljs-comment">// RunoobRunoob</span>
</code></pre>
<h3 id="10&#x3001;replace">10&#x3001;replace()</h3>
<p>&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x67E5;&#x627E;&#x5339;&#x914D;&#x7684;&#x5B57;&#x7B26;&#xFF0C;&#x5E76;&#x66FF;&#x6362;&#x4E0E;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x5339;&#x914D;&#x7684;&#x5B57;&#x7B26;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;Mr Blue has a blue house and a blue car&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.replace(<span class="hljs-string">&quot;blue&quot;</span>, <span class="hljs-string">&quot;red&quot;</span>)); <span class="hljs-comment">// Mr Blue has a red house and a blue car</span>
<span class="hljs-comment">// &#x5168;&#x5C40;&#x66FF;&#x6362;&#xFF0C;&#x5FFD;&#x7565;&#x5927;&#x5C0F;&#x5199;</span>
<span class="hljs-built_in">console</span>.log(str.replace(<span class="hljs-regexp">/blue/gi</span>, <span class="hljs-string">&quot;red&quot;</span>)); <span class="hljs-comment">// Mr red has a red house and a red car</span>
</code></pre>
<h3 id="11&#x3001;search">11&#x3001;search()</h3>
<p>&#x67E5;&#x627E;&#x4E0E;&#x6B63;&#x5219;&#x8868;&#x8FBE;&#x5F0F;&#x76F8;&#x5339;&#x914D;&#x7684;&#x503C;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;Mr. Blue has a blue house&quot;</span>;
<span class="hljs-comment">// &#x5BF9;&#x5927;&#x5C0F;&#x5199;&#x654F;&#x611F;</span>
<span class="hljs-built_in">console</span>.log(str.search(<span class="hljs-string">&quot;blue&quot;</span>)); <span class="hljs-comment">// 15</span>
<span class="hljs-built_in">console</span>.log(str.search(<span class="hljs-regexp">/blue/i</span>)); <span class="hljs-comment">// 4</span>
</code></pre>
<h3 id="12&#x3001;slice">12&#x3001;slice()</h3>
<p>&#x63D0;&#x53D6;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x5E76;&#x5728;&#x65B0;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x8FD4;&#x56DE;&#x63D0;&#x53D6;&#x7684;&#x90E8;&#x5206;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;Hello world!&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.slice(<span class="hljs-number">1</span>, <span class="hljs-number">5</span>)); <span class="hljs-comment">// ello</span>
<span class="hljs-built_in">console</span>.log(str.slice(<span class="hljs-number">-1</span>)); <span class="hljs-comment">// !</span>
</code></pre>
<h3 id="13&#x3001;split">13&#x3001;split()</h3>
<p>&#x628A;&#x5B57;&#x7B26;&#x4E32;&#x5206;&#x5272;&#x6210;&#x5B57;&#x7B26;&#x4E32;&#x6570;&#x7EC4;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;How are you doing today?&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.split(<span class="hljs-string">&quot; &quot;</span>)); <span class="hljs-comment">// [&quot;How&quot;, &quot;are&quot;, &quot;you&quot;, &quot;doing&quot;, &quot;today?&quot;]</span>
<span class="hljs-comment">// &#x8F93;&#x51FA;3&#x4E2A;&#x6570;&#x7EC4;&#x7684;&#x503C;</span>
<span class="hljs-built_in">console</span>.log(str.split(<span class="hljs-string">&quot; &quot;</span>, <span class="hljs-number">3</span>)); <span class="hljs-comment">// [&quot;How&quot;, &quot;are&quot;, &quot;you&quot;]</span>
</code></pre>
<h3 id="14&#x3001;startswith">14&#x3001;startsWith()</h3>
<p>&#x67E5;&#x770B;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x4EE5;&#x6307;&#x5B9A;&#x7684;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#x5F00;&#x5934;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;Hello world, welcome to the Runoob.&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.startsWith(<span class="hljs-string">&quot;Hello&quot;</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-comment">// &#x4ECE;&#x7B2C; 6 &#x4E2A;&#x7D22;&#x5F15;&#x4F4D;&#x7F6E;&#x5F00;&#x59CB;</span>
<span class="hljs-built_in">console</span>.log(str.startsWith(<span class="hljs-string">&quot;world&quot;</span>, <span class="hljs-number">6</span>)); <span class="hljs-comment">// true</span>
</code></pre>
<h3 id="15&#x3001;substr">15&#x3001;substr()</h3>
<p>&#x4ECE;&#x7D22;&#x5F15;&#x5F00;&#x59CB;&#x63D0;&#x53D6;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x6307;&#x5B9A;&#x6570;&#x76EE;&#x7684;&#x5B57;&#x7B26;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;Hello world!&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.substr(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>)); <span class="hljs-comment">// llo</span>
<span class="hljs-built_in">console</span>.log(str.substr(<span class="hljs-number">2</span>)); <span class="hljs-comment">// llo world!</span>
<span class="hljs-built_in">console</span>.log(str.substr(<span class="hljs-number">-2</span>)); <span class="hljs-comment">// d!</span>
</code></pre>
<h3 id="16&#x3001;substring">16&#x3001;substring()</h3>
<p>&#x63D0;&#x53D6;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x4E24;&#x4E2A;&#x7D22;&#x5F15;&#x53F7;&#x4E4B;&#x95F4;&#x7684;&#x5B57;&#x7B26;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str=<span class="hljs-string">&quot;Hello world!&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.substring(<span class="hljs-number">3</span>,<span class="hljs-number">7</span>)); <span class="hljs-comment">// lo w</span>
<span class="hljs-built_in">console</span>.log(str.substring(<span class="hljs-number">3</span>)); <span class="hljs-comment">// lo world!</span>
</code></pre>
<h3 id="17&#x3001;tolowercase">17&#x3001;toLowerCase()</h3>
<p>&#x628A;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;&#x5C0F;&#x5199;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str=<span class="hljs-string">&quot;Runoob&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.toLowerCase()); <span class="hljs-comment">// runoob</span>
</code></pre>
<h3 id="18&#x3001;touppercase">18&#x3001;toUpperCase()</h3>
<p>&#x628A;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;&#x5927;&#x5199;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str=<span class="hljs-string">&quot;Runoob&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.toUpperCase()); <span class="hljs-comment">// RUNOOB</span>
</code></pre>
<h3 id="19&#x3001;trim">19&#x3001;trim()</h3>
<p>&#x53BB;&#x9664;&#x5B57;&#x7B26;&#x4E32;&#x4E24;&#x8FB9;&#x7684;&#x7A7A;&#x767D;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;       Runoob        &quot;</span>;
<span class="hljs-built_in">console</span>.log(str.trim()); <span class="hljs-comment">// Runoob</span>
</code></pre>
<h3 id="20&#x3001;tolocalelowercase">20&#x3001;toLocaleLowerCase()</h3>
<p>&#x6839;&#x636E;&#x672C;&#x5730;&#x4E3B;&#x673A;&#x7684;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x628A;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;&#x5C0F;&#x5199;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str=<span class="hljs-string">&quot;Runoob&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.toLocaleLowerCase()); <span class="hljs-comment">// runoob</span>
</code></pre>
<h3 id="21&#x3001;tolocaleuppercase">21&#x3001;toLocaleUpperCase()</h3>
<p>&#x6839;&#x636E;&#x672C;&#x5730;&#x4E3B;&#x673A;&#x7684;&#x8BED;&#x8A00;&#x73AF;&#x5883;&#x628A;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;&#x5C0F;&#x5199;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str=<span class="hljs-string">&quot;Runoob&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.toLocaleUpperCase()); <span class="hljs-comment">// RUNOOB</span>
</code></pre>
<h3 id="22&#x3001;valueof">22&#x3001;valueOf()</h3>
<p>&#x8FD4;&#x56DE;&#x67D0;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x5BF9;&#x8C61;&#x7684;&#x539F;&#x59CB;&#x503C;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;Hello world!&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.valueOf()); <span class="hljs-comment">// Hello world!</span>
</code></pre>
<h3 id="23&#x3001;tostring">23&#x3001;toString()</h3>
<p>&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> str = <span class="hljs-string">&quot;Runoob&quot;</span>;
<span class="hljs-built_in">console</span>.log(str.toString());
</code></pre>
<h2 id="es67&#x4E2D;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x65B9;&#x6CD5;">ES6/7&#x4E2D;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x65B9;&#x6CD5;</h2>
<h3 id="1&#x3001;includes">1&#x3001;includes()</h3>
<p>&#x8FD4;&#x56DE;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x662F;&#x5426;&#x627E;&#x5230;&#x53C2;&#x6570;&#x5B57;&#x7B26;&#x4E32;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> s = <span class="hljs-string">&apos;Hello world!&apos;</span>;
<span class="hljs-built_in">console</span>.log(s.includes(<span class="hljs-string">&apos;o&apos;</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-comment">// &#x652F;&#x6301;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x5F00;&#x59CB;&#x641C;&#x7D22;&#x7684;&#x4F4D;&#x7F6E;</span>
<span class="hljs-built_in">console</span>.log(s.includes(<span class="hljs-string">&apos;o&apos;</span>, <span class="hljs-number">8</span>)) <span class="hljs-comment">// false</span>
</code></pre>
<h3 id="2&#x3001;startswith">2&#x3001;startsWith()</h3>
<p>&#x8FD4;&#x56DE;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x53C2;&#x6570;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x5728;&#x6E90;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5934;&#x90E8;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> s = <span class="hljs-string">&apos;Hello world!&apos;</span>;
<span class="hljs-built_in">console</span>.log(s.startsWith(<span class="hljs-string">&quot;Hello&quot;</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-comment">// &#x652F;&#x6301;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x5F00;&#x59CB;&#x641C;&#x7D22;&#x7684;&#x4F4D;&#x7F6E;</span>
<span class="hljs-built_in">console</span>.log(s.startsWith(<span class="hljs-string">&quot;world&quot;</span>, <span class="hljs-number">1</span>)); <span class="hljs-comment">// false</span>
</code></pre>
<h3 id="3&#x3001;endswith">3&#x3001;endsWith()</h3>
<p>&#x8FD4;&#x56DE;&#x5E03;&#x5C14;&#x503C;&#xFF0C;&#x8868;&#x793A;&#x53C2;&#x6570;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x5728;&#x6E90;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5C3E;&#x90E8;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> s = <span class="hljs-string">&apos;Hello world!&apos;</span>;
<span class="hljs-built_in">console</span>.log(s.endsWith(<span class="hljs-string">&quot;world!&quot;</span>)); <span class="hljs-comment">// true</span>
<span class="hljs-comment">// &#x652F;&#x6301;&#x7B2C;&#x4E8C;&#x53C2;&#x6570;&#xFF0C;&#x8868;&#x793A;&#x6307;&#x5B9A;&#x8981;&#x641C;&#x7D22;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x957F;&#x5EA6;</span>
<span class="hljs-built_in">console</span>.log(s.endsWith(<span class="hljs-string">&quot;world!&quot;</span>, s.length)); <span class="hljs-comment">// true</span>
</code></pre>
<h3 id="4&#x3001;repeat">4&#x3001;repeat()</h3>
<p>&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x65B0;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8868;&#x793A;&#x5C06;&#x539F;&#x5B57;&#x7B26;&#x4E32;&#x91CD;&#x590D;n&#x6B21;</p>
<pre><code class="lang-js"><span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;a&apos;</span>.repeat(<span class="hljs-number">3</span>)); <span class="hljs-comment">// aaa</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;a&apos;</span>.repeat(<span class="hljs-number">0</span>)); <span class="hljs-comment">//</span>
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;a&apos;</span>.repeat(<span class="hljs-number">2.6</span>)); <span class="hljs-comment">// aa</span>
</code></pre>
<ul>
<li>&#x6CE8;&#x610F;&#xFF1A;</li>
<li>&#x5982;&#x679C;&#x53C2;&#x6570;&#x662F;&#x5C0F;&#x6570;&#xFF0C;&#x4F1A;&#x53D6;&#x6574;</li>
<li>&#x5982;&#x679C;&#x53C2;&#x6570;&#x662F;0&#xFF0C;&#x4F1A;&#x5F97;&#x5230;&#x7A7A;&#x4E32;</li>
<li>&#x5982;&#x679C;&#x53C2;&#x6570;&#x662F;&#x8D1F;&#x6570;&#x6216;&#x8005;Infinity&#xFF0C;&#x4F1A;&#x62A5;&#x9519;</li>
<li>&#x5982;&#x679C;&#x53C2;&#x6570;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x5E03;&#x5C14;&#x578B;&#x6216;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#xFF0C;&#x4F1A;&#x5148;&#x6362;&#x7B97;&#x6210;&#x6574;&#x6570;</li>
</ul>

                                
                                </section>
                            
                        </div>
                    </div>
                
            </div>

            
                
                <a href="数据类型.html" class="navigation navigation-prev " aria-label="Previous page: 数据类型">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="数组.html" class="navigation navigation-next " aria-label="Next page: 数组">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"字符串","level":"1.4.2","depth":2,"next":{"title":"数组","level":"1.4.3","depth":2,"path":"md/03js/数组.md","ref":"./md/03js/数组.md","articles":[]},"previous":{"title":"数据类型","level":"1.4.1","depth":2,"path":"md/03js/数据类型.md","ref":"./md/03js/数据类型.md","articles":[]},"dir":"ltr"},"config":{"plugins":["-search","-sharing","toggle-chapters","image-captions","-anchor-navigation","-anchor-navigation-ex"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"image-captions":{"attributes":{"style":"left: 50%;position: relative;transform: translateX(-50%);"},"caption":"图 _PAGE_LEVEL_._PAGE_IMAGE_NUMBER_ - _CAPTION_","align":"center","variable_name":"_pictures"},"fontsettings":{"theme":"night","family":"sans","size":2},"toggle-chapters":{},"highlight":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Landen","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{"_pictures":[]},"gitbook":"3.x.x"},"file":{"path":"md/03js/字符串.md","mtime":"2020-06-16T15:42:56.220Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-08-17T10:41:18.504Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-toggle-chapters/toggle.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

